package com.yc.testException;

import java.io.IOException;

public class Test {
	
	/**
	 * 结论：只有try，异常不会被处理，会继续往上抛
	 */
	public void testTryFinally() {
		TryCatch tc = new TryCatch();
		try {
			tc.testTryFinally();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void testTryFinally2() {
		TryCatch tc = new TryCatch();
		tc.testTryFinally2();
	}
	
	/**
	 * 结论：
	 * new TestRuntimeException(e);把catch的异常又往上抛。就会出现常见的“多段”异常堆栈
	 * new TestRuntimeException();抛了一个新异常（和捕获的无关）
	 * new TestRuntimeException("错误信息");抛了一个新异常（和捕获的无关），并且增加了一句说明
	 */
	public void testNullPoint() {
		TryCatch tc = new TryCatch();
		tc.testNullPoint();
	}
	
	public void testNullPoint2() {
		TryCatch tc = new TryCatch();
		tc.testNullPoint2();
	}
	
	public void testNullPoint3() {
		TryCatch tc = new TryCatch();
		tc.testNullPoint3();
	}
	
	public void m2() {
		try {
			int a = 2/0;
		}catch (Exception e) {
//			e.printStackTrace();
			StackTraceElement[] arr = e.getStackTrace();
			System.out.println(e.toString());
			
			for (StackTraceElement stackTraceElement : arr) {
				System.out.println("    " + stackTraceElement.toString());
			}
		}
	}

	/**
	 * 研究问题：捕获其他类的异常，是否会出现“堆栈标记错误位置有误差”
	 */
	public void m3() {
		try {
			System.out.println("-----m3-----1");
			TryCatch tc = new TryCatch();
			tc.testExc4();
			System.out.println("-----m2-----2");
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("-----m3-----4");
	}

	public int m4() {
		int a = 0;
		try {
			System.out.println(a/0);
			a = 2;
		} catch (Exception e) {
			a = 3;
			return a;
		} finally {
			a = 4;
		}
		return a;
	}

	public String m5() {
		StringBuffer a = new StringBuffer();
		try {
			System.out.println(2/0);
			a.append("aaa");
		} catch (Exception e) {
			a.append("bbb");
			return a.toString();
		} finally {
			a.append("ccc");
		}
		return a.toString();
	}

	public static void main(String[] args) {
		Test t = new Test();
//		t.testTryFinally();
//		t.testTryFinally2();
//		t.testNullPoint();
//		t.testNullPoint2();
//		t.testNullPoint3();
//		t.m2();
//		t.m3();
//		System.out.println(t.m4());
		System.out.println(t.m5());
	}

}
